﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELS.User;

namespace ELS.Template
{
    public partial class AddEditTemplateForm : Form
    {
        Panel parentPanel;
        TextBox textBox;
        PictureBox pictureBox;
        TemplateController templateController = new TemplateController();
        bool isAddNewTemplate = true;
        string updatedTemplateID;
        public AddEditTemplateForm(TextBox textBox, PictureBox pictureBox, Panel pnlTemplateForm)
        {
            this.textBox = textBox;
            this.pictureBox = pictureBox;
            parentPanel = pnlTemplateForm;
            isAddNewTemplate = true;

            InitializeComponent();
            templateController.LoadAllDocumentTypesAndDefault(targetBox);
        }

        public AddEditTemplateForm(string selectedTemplateID, TextBox textBox, PictureBox pictureBox, Panel pnlTemplateForm)
        {
            this.textBox = textBox;
            this.pictureBox = pictureBox;
            parentPanel = pnlTemplateForm;
            isAddNewTemplate = false;
            updatedTemplateID = selectedTemplateID;

            InitializeComponent();
            templateController.LoadAllDocumentTypes(targetBox);
            LoadSelectedTemplateInfo(selectedTemplateID);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (nameBox.Text.Trim().Equals(""))
            {
                UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.AddEditTemplate_DoNotEnterTemplateTitle, UserCommon.WARNING_STATUS);
                return;
            }

            if (int.Parse(targetBox.SelectedValue.ToString()) == 0) {
                UserCommon.NotifyOnMainForm(textBox, pictureBox, UserCommon.AddEditTemplate_DoNotSelectDocumentType, UserCommon.WARNING_STATUS);
                return;
            }

            int templateID = -1;
            TemplateDocument td = new TemplateDocument();
            td.Title = nameBox.Text;
            td.Description = descriptionBox.Text;
            td.DocumentTypeId = ""+ targetBox.SelectedValue;
            td.ModifiedBy = ""+ UserCommon.loggedInUserID;

            if (isAddNewTemplate)
            {
                td.CreatedBy = ""+ UserCommon.loggedInUserID;
                templateID = templateController.InsertTemplate(td);
            }
            else {
                td.Id = updatedTemplateID;
                templateID = templateController.UpdateTemplate(td);
            }

            foreach (Control ctl in parentPanel.Controls)
            {
                ctl.Dispose();
            }

            AddEditQuestionForm addEditTemplateForm = new AddEditQuestionForm(textBox, pictureBox, parentPanel);
            addEditTemplateForm.TopLevel = false;
            addEditTemplateForm.Parent = parentPanel;
            addEditTemplateForm.templateName = td.Title;
            addEditTemplateForm.templateId = templateID;
            addEditTemplateForm.Visible = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in parentPanel.Controls)
            {
                ctl.Dispose();
            }

            ListTemplateForm manageForm = new ListTemplateForm(textBox, pictureBox, parentPanel);
            manageForm.TopLevel = false;
            manageForm.Parent = parentPanel;
            manageForm.Visible = true;
        }

        private void LoadSelectedTemplateInfo(string selectedTemplateID)
        {
            TemplateDocument updatedTemplate = templateController.GetTemplateInfo(selectedTemplateID);
            nameBox.Text = updatedTemplate.Title;
            descriptionBox.Text = updatedTemplate.Description;
            targetBox.SelectedValue = int.Parse(updatedTemplate.DocumentTypeId);
        }
    }
}
